package com.dongdongshop.controller;

import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.service.ContentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@RequestMapping
@Controller
public class IndexController {
    Logger logger = LoggerFactory.getLogger(IndexController.class);
    /*
    *   首页轮播广告 1
        今日推荐 2
        活动专区 3
        猜你喜欢 4
        服装楼层广告 5
    * */
    @DubboReference
    private ContentService contentService;
    @RequestMapping
    public String runIndex(){
        return "index";
    }
    //首页轮播广告,
    /*
    * 参数
    * 轮播广告数量 pageSize 默认5
    * 首页轮播广告类型Id
    * */
    @RequestMapping("rotationAdvertisement")
    @ResponseBody
    public AjaxResult indexPageAdvertisement(@RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize, Long categoryId){
        try {
            List<TbContent> listContent = contentService.selectIndexPageAdvertisement(pageSize,categoryId);
            if (listContent.size()>0){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(listContent);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("首页轮播广告查询失败!错误详情:{}",e);
            return AjaxResult.R(ResultEnum.ERROR);
        }
    }
    @RequestMapping("weather")
    @ResponseBody
    public String showWeather() throws IOException {
        //        1.创建Httpclient对象,相当于打开了浏览器
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //        2.创建http请求 get、post
        //创建doget请求,相当于浏览器输入地址
        HttpGet httpGet = new HttpGet("http://wthrcdn.etouch.cn/weather_mini?city=北京");
        //        3.执行请求，得到响应对象
        CloseableHttpResponse response= null;
        String content = null;
        try {
            response = httpclient.execute(httpGet);
            //        4.判断响应状态码是不是200，获取响应数据实体对象,200表示响应成功,一般用于get和post请求
            if (response.getStatusLine().getStatusCode() == 200){
                //        5.获取响应数据
                HttpEntity entity = response.getEntity();
                content = EntityUtils.toString(entity, "utf-8");
                System.out.println("响应结果为---->"+content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //        6.关闭响应对象
            if (response != null){
                response.close();
            }
            //关闭httpclient
            httpclient.close();
        }
        return content;
    }

}
